package com.example.design.patterns.geekbang.创建型.单例模式.demo;

import java.io.File;
import java.io.FileWriter;
import lombok.SneakyThrows;

/**
 * 饿汉模式，单例实例在类装载的时候进行创建，线程安全
 */
public class Logger {
    private static final Logger instance = new Logger();
    private FileWriter writer;

    @SneakyThrows
    public Logger() {
        File file = new File("/Users/wangzheng/log.txt");
        writer = new FileWriter(file, true); //true表示追加写入
    }

    public static Logger getInstance() {
        return instance;
    }

    @SneakyThrows
    public void log(String message) {
        //synchronized (Logger.class) {// 类级别的锁
        writer.write(message);
        //}
    }
}



